<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Message extends MY_Controller {

	function Message()
	{
		parent::MY_Controller();
        $this->load->helper('page');
	}

	function index(){
		self::view(0);
	}


    function add() {
        $auth_code = $this->input->post('auth_code');
        $session_code = parent::get_session_value('message_code');
        if(!$auth_code) {
            parent::redirect('请填写验证码！','back',3);
        } elseif (strtoupper($auth_code) != $session_code){
            parent::redirect('验证码不正确！请返回重新填写','back',3);
        }
        $form = array('author','title','qq','content');
        foreach($form as $val){
            $$val = $this->input->post($val);
        }
        if(!$content || !$title) {
            parent::redirect('主题和内容都必须填写','back',3);
        }
        $values = array();
        foreach($form as $val){
            $values[$val] = $$val;
        }
        $values['add_time'] = date('Y-m-d H:i:s');
        $values['state']    = 1;
        $values['ip']       = $this->input->ip_address();
        $this->load->model('Mmessage');
        if($this->Mmessage->insert_new_message($values)) {
            parent::redirect('留言发表成功！',base_url().'message/view/',3);
        }
    }


    function view($offset=0) {
        page_common();
        $this->load->model('Mmessage');

        $total = $this->Mmessage->get_message_count();

		$this->load->library('pagination');

		$config['base_url'] = base_url().'message/view/';
		$config['total_rows'] = $total;
		$config['per_page'] = 5;
		$config['uri_segment'] = 3;
		$config['num_links'] = 3;
		$config['full_tag_open'] = '<div class="pagebar">';
		$config['full_tag_close'] = '</div>';
		$config['cur_tag_open'] = '<span class="current">';
		$config['cur_tag_close'] = '</span>';
		$config['first_link'] = '&#171;';
		$config['last_link'] = '&#187;';
		$config['next_link'] = '&gt;';
		$config['prev_link'] = '&lt;';

		$this->pagination->initialize($config);

		$page_html = $this->pagination->create_links();

        $messages = $this->Mmessage->get_message_list($offset,$config['per_page']);

        $this->template->view('v1/message_view',array('messages'=>$messages,'page_html'=>$page_html));
    }

    function write() {
        page_common();
        $this->template->view('v1/message_write');
    }
}

?>